package com.stoneframe.controller.manageAuthorize;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.stoneframe.controller.base.BaseController;
import com.stoneframe.entity.other.TreeEntity;
import com.stoneframe.entity.other.ZTreeEntity;
import com.stoneframe.service.manageAuthorize.SysModuleButtonService;
import com.stoneframe.service.manageAuthorize.SysModuleService;
import com.stoneframe.util.baseApp.AppHelper;
import com.stoneframe.util.baseApp.PageData;
import com.stoneframe.util.baseApp.ToolHelper;
import com.stoneframe.util.baseJson.TreeJsonHelper;


@Controller
@RequestMapping(value="sysModule")
public class SysModuleController extends BaseController {
	
	private String controllerName="SysModuleController.";
	
	@Resource(name="sysModuleService")
	private SysModuleService sysModuleService;
	
	@Resource(name="sysModuleButtonService")
	private SysModuleButtonService sysModuleButtonService;
	
	
	@RequestMapping(value="index")
	public ModelAndView index(){
		logBefore(logger, controllerName+"index");
		
		ModelAndView mv=this.getModelAndView();
		PageData pd=this.getPageData();
		mv.addObject("pd",pd);
		mv.setViewName("manageAuthorize/sysModule/index");
		
		logAfter(logger);
		return mv;
	}
	
	/**
	 * 树JONS
	 */
	@RequestMapping(value="treeJson",produces= "text/plain;charset=UTF-8")
	@ResponseBody
	public Object treeJson(){
		logBefore(logger, controllerName+"treeJson(树)");
		
		PageData pd=this.getPageData();
		List<PageData> list=null;
		List<TreeEntity> treeList = new ArrayList();
		try{
			list=sysModuleService.listAll(pd);
			for(PageData sysModulePageData:list){
				TreeEntity tree=new TreeEntity();
				Boolean hasChildren =false;
				for(PageData subModulePageData:list){
					if(subModulePageData.getString("parentId").equals(sysModulePageData.getString("moduleId"))){
						hasChildren=true;
						break;
					}
				}
                tree.setId(sysModulePageData.getString("moduleId"));
                tree.setText(sysModulePageData.getString("moduleName"));
                tree.setValue(sysModulePageData.getString("moduleId"));
                tree.setIsexpand(true);
                tree.setComplete(true);
                tree.setHasChildren(hasChildren);
                tree.setParentId(sysModulePageData.getString("parentId"));
                tree.setImg(sysModulePageData.getString("moduleIcon"));
				
				treeList.add(tree);
			}
			
		}catch(Exception e){
			logger.error(e.toString(),e);
			return AppHelper.errorObject(e.getMessage());
		}

		logAfter(logger);
		return AppHelper.returnObject(pd,TreeJsonHelper.treeToJson(treeList, "0"));
	}
	
	/**
	 * 获取所有记录
	 */
	@RequestMapping(value="listAll")
	@ResponseBody
	public Object listAll(){
		logBefore(logger, controllerName+"listAll");
		PageData pd=this.getPageData();
		List<PageData> list=new ArrayList<PageData>();
		try{
			list=sysModuleService.listAll(pd);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return AppHelper.errorObject(e.getMessage());
		}
		logAfter(logger);
		return AppHelper.successObject(list);
	}
	
	/**
	 * 获取详情
	 */
	@ResponseBody
	@RequestMapping(value="get")
	public Object get(){
		logBefore(logger, controllerName+"get");
		PageData pd=this.getPageData();
		try{
			pd=sysModuleService.findById(pd);
			return AppHelper.successObject(pd);
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return AppHelper.errorObject("获取失败；"+e.getMessage());
		}
	}
	
	/**
	 * 更新记录
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	public Object edit(){
		logBefore(logger, controllerName+"edit");
		PageData pd=this.getPageData();
		
		try{
			if(ToolHelper.notEmpty(pd.getString("keyValue"))){
				sysModuleService.edit(pd);
			}else{
				pd.put("keyValue", this.get32Guid());
				sysModuleService.save(pd);
			}
			return AppHelper.successObject("操作成功！");
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return AppHelper.errorObject("操作失败！"+e.getMessage());
		}
	}
	
	/**
	 * 删除记录
	 */
	@ResponseBody
	@RequestMapping(value="del")
	public Object del(){
		logBefore(logger, controllerName+"del");
		PageData pd=this.getPageData();
		try{
			sysModuleService.del(pd);
			return AppHelper.successObject("删除成功！");
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return AppHelper.errorObject("删除失败:"+e.getMessage());
		}
	}
	
	/**
	 * 删除记录
	 */
	@ResponseBody
	@RequestMapping(value="saveForm")
	public Object saveForm(String keyValue, String moduleButtonListJson, String moduleColumnListJson){
		logBefore(logger, controllerName+"saveForm");
		PageData pd=this.getPageData();
		try{
			if(ToolHelper.notEmpty(keyValue)){ //修改
				sysModuleService.edit(pd);
				
				sysModuleButtonService.del(new PageData("moduleId",keyValue));
				List<Map<String,Object>> buttonList=new ObjectMapper().readValue(moduleButtonListJson, List.class);
				for(Map<String,Object> item:buttonList){
					PageData buttonData=new PageData();
					buttonData.put("buttonId", item.get("buttonId"));
					buttonData.put("moduleId", item.get("moduleId"));
					buttonData.put("parentId", item.get("parentId"));
					buttonData.put("buttonIcon", item.get("buttonIcon"));
					buttonData.put("buttonName", item.get("buttonName"));
					buttonData.put("actionAddress", item.get("actionAddress"));
					buttonData.put("sortCode", item.get("sortCode"));
					sysModuleButtonService.save(buttonData);
				}
			}else{
				String moduleId=this.get32Guid();
				pd.put("moduleId", moduleId);
				sysModuleService.save(pd);
				List<Map<String,Object>> buttonList=new ObjectMapper().readValue(moduleButtonListJson, List.class);
				for(Map<String,Object> item:buttonList){
					PageData buttonData=new PageData();
					buttonData.put("buttonId", item.get("buttonId"));
					buttonData.put("moduleId", moduleId);
					buttonData.put("parentId", item.get("parentId"));
					buttonData.put("buttonIcon", item.get("buttonIcon"));
					buttonData.put("buttonName", item.get("buttonName"));
					buttonData.put("actionAddress", item.get("actionAddress"));
					buttonData.put("sortCode", item.get("sortCode"));
					sysModuleButtonService.save(buttonData);
				}
				
			}
			logAfter(logger);
			return AppHelper.successObject("保存成功");
		}catch(Exception e){
			logger.error(e.toString(), e);
			return AppHelper.errorObject(e.getMessage());
		}
		
	}

}
